let store = 0
import axios from '@/utils/request'

export default {
  StorePenalty: {
    entity: { name: 'Penalty', prefix: () => {
      store = localStorage.getItem('storeId')
      return `store/${store}`
    } },
    list: {
      disabled_actions: ['edit', 'delete'],
      actions: [
        { name: 'sync',
          position: 'list',
          component: {
            props: ['record', 'refresh'],
            data() {
              return {
                loading: false
              }
            },
            methods: {
              perform(id) {
                this.loading = true
                axios.post(`/store/${store}/penalties/${id}/perform`)
                  .then((res) => {
                    this.$message.success('扣款成功!')
                    this.refresh()
                  })
                  .catch((err) => {
                    console.error(err)
                  })
                  .finally(() => { this.loading = false })
              }
            },
            render(h) {
              if (this.record.performed < 1) {
                return (
                  <el-button nativeOnClick={ () => { this.perform(this.record.id) } }
                    slot='reference' size='small' loading={this.loading}>
                          执行扣款
                  </el-button>
                )
              }
            }
          }
        }
      ],
      list_filter: {
        name: {
          expression: `entity.getStaff().getName() matches ":value"`,
          label: '名称',
          type: 'input'
        },
        beforeCreatedTime: {
          expression: 'entity.getCreatedTime() >= datetime.get(":value  00:00:00")',
          label: '开始时间',
          type: 'date'
        },
        afterCreatedTime: {
          expression: 'entity.getCreatedTime() < datetime.get(":value 23:59:59")',
          label: '结束时间',
          type: 'date'
        }
      },
      list_display: [
        'staff',
        { property: 'forfeit', label: '扣罚金额' },
        'comment',
        { property: 'performed', label: '扣罚状态' },
        { property: 'createdTime', type: 'datetime' }
      ]
    },
    form: {
      fields: [
        'staff',
        { property: 'forfeit', field_options: { label: '扣罚金额' }},
        { property: 'comment', type: 'textarea' }
      ]
    }
  }
}

